<template>
  <div class="dashboard-container">
    <div class="container">
      <div class="tableBar">
        <div class="search-box">
          <el-input 
            v-model="description" 
            placeholder="请输入题目题干" 
            clearable
            style="width: 200px; margin-right: 15px;"
            @keyup.enter.native="pageQuery"
          >
            <template slot="prepend">题目题干</template>
          </el-input>
          <el-button 
            type="primary" 
            icon="el-icon-search"
            @click="pageQuery"
          >
            查询
          </el-button>
        </div>
        
        <div class="action-buttons">
          <el-button 
            type="danger" 
            icon="el-icon-delete" 
            :disabled="selectedIds.length === 0"
            @click="batchDelete"
          >
            批量删除
          </el-button>
          <el-button 
            type="primary" 
            icon="el-icon-plus" 
            @click="handleAddEmp"
          >
            添加题目
          </el-button>
        </div>
      </div>
      
      <div class="batch-action" v-if="selectedIds.length > 0">
        <span>已选择 {{ selectedIds.length }} 道题目</span>
        <el-button 
          type="text" 
          icon="el-icon-close" 
          @click="clearSelection"
        >
          取消选择
        </el-button>
      </div>
      
      <el-table 
        :data="records"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="loading"
        ref="questionTable"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        
        <el-table-column
          prop="description"
          label="题目题干"
          width="280">
        </el-table-column>
        
        <el-table-column
          prop="optiona"
          label="选项A"
          width="180">
        </el-table-column>
        
        <el-table-column
          prop="optionb"
          label="选项B"
          width="180">
        </el-table-column>
        
        <el-table-column
          prop="ansera"
          label="选项A对应人格"
          width="80">
        </el-table-column>
        
        <el-table-column
          prop="anserb"
          label="选项B对应人格"
          width="80">
        </el-table-column>
        
        <el-table-column
          label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="handleUpdataEmp(scope.row)">修改</el-button>
            <el-button 
              type="text" 
              style="color: #F56C6C;"
              @click="singleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="empty-state" v-if="records.length === 0 && !loading">
        <img src="https://img.icons8.com/ios/100/000000/nothing-found.png" alt="No data">
        <p>暂无题目数据，请添加题目</p>
      </div>
      
      <el-pagination
        class="pageList"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script lang="ts">
import { getQuestion, deleteBatchQuestions } from '@/api/question'

export default {
  data() {
    return {
      description: '',        // 题目描述
      page: 1,               // 页码
      pageSize: 10,          // 页数
      total: 0,              // 总数量
      records: [],           // 题目列表
      loading: false,        // 加载状态
      selectedIds: []        // 选中的题目ID
    }
  },
  
  created() {
    this.pageQuery()
  },
  
  methods: {
    pageQuery() {
      this.loading = true
      const param = { 
        description: this.description,
        page: this.page,
        pageSize: this.pageSize 
      }
      
      getQuestion(param)
        .then(res => {
          if (res.data.code === 1) {
            this.total = res.data.data.total
            this.records = res.data.data.records
          }
          this.loading = false
        })
        .catch(err => {
          this.$message.error('请求出错：' + err.message)
          this.loading = false
        })
    },
    
    // 处理选择变化
    handleSelectionChange(selection) {
      this.selectedIds = selection.map(item => item.id)
    },
    
    // 清除选择
    clearSelection() {
      this.$refs.questionTable.clearSelection()
      this.selectedIds = []
    },
    
    // 批量删除题目
    batchDelete() {
      if (this.selectedIds.length === 0) {
        this.$message.warning('请选择要删除的题目')
        return
      }
      
      this.$confirm(
        `确定要删除选中的 ${this.selectedIds.length} 道题目吗？`,
        '删除确认',
        {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }
      )
      .then(() => {
        deleteBatchQuestions(this.selectedIds)
          .then(res => {
            if (res.data.code === 1) {
              this.$message.success('删除成功')
              this.pageQuery()
              this.selectedIds = []
            } else {
              this.$message.error(res.data.message || '删除失败')
            }
          })
          .catch(err => {
            this.$message.error('删除失败: ' + err.message)
          })
      })
      .catch(() => {
        this.$message.info('已取消删除')
      })
    },
    
    // 单个删除题目
    singleDelete(row) {
      this.$confirm(
        `确定要删除题目 "${row.description}" 吗？`,
        '删除确认',
        {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }
      )
      .then(() => {
        deleteBatchQuestions([row.id])
          .then(res => {
            if (res.data.code === 1) {
              this.$message.success('删除成功')
              this.pageQuery()
            } else {
              this.$message.error(res.data.message || '删除失败')
            }
          })
          .catch(err => {
            this.$message.error('删除失败: ' + err.message)
          })
      })
      .catch(() => {
        this.$message.info('已取消删除')
      })
    },
    
    // pageSize发生变化时
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.pageQuery()
    },
    
    // 页面变化时触发
    handleCurrentChange(page) {
      this.page = page
      this.pageQuery()
    },
    
    // 跳转到新增题目组件
    handleAddEmp() {
      this.$router.push('/question/add')
    },
    
    // 修改题目组件（跳转）
    handleUpdataEmp(row) {
      this.$router.push({
        path: '/question/add',
        query: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  max-width: 1400px;
  margin: 20px auto;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.container {
  padding: 25px 30px;
}

.tableBar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px 20px;
  background: #f9fafc;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.search-box {
  display: flex;
  align-items: center;
  flex-grow: 1;
}

.action-buttons {
  display: flex;
  gap: 15px;
}

.batch-action {
  display: flex;
  align-items: center;
  background: #f0f7ff;
  padding: 10px 15px;
  border-radius: 6px;
  margin-bottom: 20px;
  border: 1px solid #d9ecff;
  transition: all 0.3s ease;
  
  span {
    color: #606266;
    margin-right: 15px;
    font-weight: 500;
  }
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  
  th {
    background-color: #f5f7fa !important;
    color: #606266;
    font-weight: 600;
  }
}

.pageList {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.empty-state {
  text-align: center;
  padding: 40px 0;
  
  img {
    width: 120px;
    opacity: 0.7;
    margin-bottom: 20px;
  }
  
  p {
    color: #909399;
    font-size: 14px;
  }
}

@media (max-width: 768px) {
  .tableBar {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .search-box {
    width: 100%;
    margin-bottom: 15px;
  }
  
  .action-buttons {
    width: 100%;
    justify-content: flex-end;
  }
}

.disabled-text {
  color: #bac0cd !important;
}
</style>